package jj.rmirm.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

public class Frame extends JFrame {

	public Frame() {
		super();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("RMI Registry Monitor " + VERSION + " by Jay");
		
		Container contentPane = this.getContentPane();
		this.setGuiComponents(contentPane);
		this.pack();
		
		//set location to center of the screen
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension scrSize = toolkit.getScreenSize();
		this.setLocation((scrSize.width / 2) - (this.getWidth() / 2), 
				(scrSize.height / 2) - (this.getHeight() / 2));
		
		this.setVisible(true);
		
		int n = JOptionPane.showConfirmDialog(
			    this,
			    "This software still needs BETA testing\n" +
			    "and its not recommended to use it in production environment.\n\n" +
			    "Do you want to use RMI Registry Monitor BETA at your own risk?",
			    "Beta message",
			    JOptionPane.YES_NO_OPTION);

		if(n != 0 )
			System.exit(0);
	}
	
	private void setGuiComponents(Container contentPane) {
		
		contentPane.setLayout(new BorderLayout());
		
		objectList = new JList();
		objectList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		objectList.setLayoutOrientation(JList.VERTICAL);
		objectList.setVisibleRowCount(10);
		
		this.objDisplayer = new ObjListDisplay(objectList);
		
		objectList.addMouseListener(new ObjListListener());
		JScrollPane listScroller = new JScrollPane(objectList);
		
		logArea = new JTextArea();
		logArea.setRows(5);
		JScrollPane logScroller = new JScrollPane(logArea); 
		logArea.setEditable(false);
		
		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                                   listScroller, logScroller);
        splitPane.setResizeWeight(0.5);
        splitPane.setOneTouchExpandable(true);
        splitPane.setContinuousLayout(true);

        contentPane.add(splitPane, BorderLayout.CENTER);
        
        toolBar = new ToolBar(this.logArea, this.objDisplayer);
		contentPane.add(toolBar, BorderLayout.NORTH);
	}
	
	private static final long serialVersionUID = -8561732520757349677L;
	
	private static final String VERSION = "0.35";
	
	private JList objectList;
	private JTextArea logArea;
	private ToolBar toolBar;
	private PopMenu popMenu = new PopMenu();
	private ObjListDisplay objDisplayer;
	
	class ObjListListener implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent me) {

			if(me.getButton() == MouseEvent.BUTTON1)
				objDisplayer.setSelectedIdx(objectList.getSelectedIndex());
		}

		@Override
		public void mouseEntered(MouseEvent me) {
			
		}

		@Override
		public void mouseExited(MouseEvent me) {

		}

		@Override
		public void mousePressed(MouseEvent me) {
			showMenu(me);
		}

		@Override
		public void mouseReleased(MouseEvent me) {
			showMenu(me);
		}
		
		private void showMenu(MouseEvent me) {
			objDisplayer.canUpdate(false);
			
			if(me.isPopupTrigger() && objectList.getSelectedIndex() >= 0) {
				
				int selectedIdx = objectList.getSelectedIndex();
				
				//make sure that user clicked right cell with object name 
				if(me.getX() >= objectList.getCellBounds(selectedIdx, selectedIdx).x &&
				   me.getX() <= objectList.getCellBounds(selectedIdx, selectedIdx).width &&
				   me.getY() >= objectList.getCellBounds(selectedIdx, selectedIdx).y &&
				   me.getY() <= objectList.getCellBounds(selectedIdx, selectedIdx).height) {
					
					objDisplayer.setSelectedIdx(selectedIdx);
					popMenu.setClickedString(objectList.getSelectedValue().toString());
					popMenu.show(me.getComponent(), me.getX(), me.getY());
				}//if getX, getY
			}//if PopupTrigger
			objDisplayer.canUpdate(true);
			
		}//private void showMenu(MouseEvent me)
	}//class ObjListListener
}
